﻿package com.webdesign.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.webdesign.service.ITbUserhomeworkService;
import com.webdesign.util.UnZipUtil;



@Controller("StuSubmitHomeworkAction")
@Scope("prototype")
public class StuSubmitHomeworkAction{
	private static final String REDIRECT = "redirect";
	private ITbUserhomeworkService iTbUserhomeworkService;
	//作业号
	private String homeworkid;
	//上传的文件
	private File upfile;
	//上传的文件名
	private String upfileFileName;
	//上传的文件类型
	private String upfileContentType;
	//上传作业提示信息,值为success表示提交成功,值为fail表示提交失败
	private String msg;
	
	/*
	 * 学生提交作业
	 */
	public String submitHomework(){
		InputStream fs = null;
		//zipmsg,解压缩zip文件提示信息，布尔值，为true表示解压缩成功，为false表示解压缩失败
		Boolean zipmsg = false;
		try {
			fs = new FileInputStream(upfile);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		UnZipUtil uz = new UnZipUtil();
		try {
			zipmsg = uz.unZipFiles(homeworkid,upfile,upfileFileName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//解压成功
		if(zipmsg){
			//创建session
			Map<String, Object> session = ActionContext.getContext().getSession();
			String uid = (String)session.get("userid");
			Boolean flag = iTbUserhomeworkService.submitHomework(homeworkid,uid);
			//提交成功
			if(flag){
				msg = "success";
			}
			//提交失败
			else{
				msg = "fail";
			}
		}
		//解压失败
		else{
			msg = "wrong";
		}
		return "success";
	}	
	

	public ITbUserhomeworkService getiTbUserhomeworkService() {
		return iTbUserhomeworkService;
	}
	@Resource(name="TbUserhomeworkService")
	public void setiTbUserhomeworkService(
			ITbUserhomeworkService iTbUserhomeworkService) {
		this.iTbUserhomeworkService = iTbUserhomeworkService;
	}


	public File getUpfile() {
		return upfile;
	}


	public void setUpfile(File upfile) {
		this.upfile = upfile;
	}


	public String getUpfileFileName() {
		return upfileFileName;
	}


	public void setUpfileFileName(String upfileFileName) {
		this.upfileFileName = upfileFileName;
	}


	public String getUpfileContentType() {
		return upfileContentType;
	}
	
	
	public void setUpfileContentType(String upfileContentType) {
		this.upfileContentType = upfileContentType;
	}


	public String getHomeworkid() {
		return homeworkid;
	}


	public void setHomeworkid(String homeworkid) {
		this.homeworkid = homeworkid;
	}


	public String getMsg() {
		return msg;
	}


	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	
	
}
